const app = getApp();
const { AskPage } = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
    /**
     * 页面的初始数据
     */
    name: "transfer-org",
    mixins: [formMixin],
    data: {
        actives: [],
        activeItems: [],
        items: [],
    },
    onShow() {
        this.initData();
    },
    onChange(e) {
        let item = e.detail;
        let values = this.data.actives || [];
        let valueItems = this.data.activeItems || [];
        let _index = values.findIndex(cur => cur === item.value);
        let _com = this.selectComponent("#askTree");
        if (_index === -1) {
            values.push(item.value);
            valueItems.push(item);
            _com.setChecked(item.value, true);
        } else {
            values.splice(_index, 1);
            valueItems.splice(_index, 1);
            _com.setChecked(item.value, false);
        }
        this.setData({
            actives: values,
            activeItems: valueItems,
        });
    },
    handleClick(e) {
        let { item } = e.currentTarget.dataset;
        this.onChange({ detail: item });
    },
    getInitParams() {
        return {
            url: "task/range",
            data: {},
        };
    },
    async initData() {
        let otherParams = this.getInitParams();
        // let { stateValue, filterValue, searchText, addByTime } = this.data.model;
        try {
            const response = await this.$_server(otherParams.url, {
                data: {
                    ...otherParams.data,
                },
            });
            let { data } = response;
            if (data.code !== 200) {
                throw data;
            }
            let { data: list } = data;
            // const list = [
            //     {
            //         deptName: "部门",
            //         users: [
            //             {
            //                 name: "我是人我是人我是人",
            //                 id: 1,
            //             },
            //             {
            //                 name: "我是人2",
            //                 id: 2,
            //             },
            //             {
            //                 name: "我是人3",
            //                 id: 3,
            //             },
            //             {
            //                 name: "我是人2",
            //                 id: 4,
            //             },
            //             {
            //                 name: "我是人3",
            //                 id: 5,
            //             },
            //             {
            //                 name: "我是人3",
            //                 id: 6,
            //             },
            //             {
            //                 name: "我是人2",
            //                 id: 7,
            //             },
            //             {
            //                 name: "我是人3",
            //                 id: 8,
            //             },
            //         ],
            //     },
            // ];
            const _list = [];
            const _activeItems = [];
            for (let i = 0; i < list.length; i++) {
                const cur = list[i];
                const _cur = {
                    label: cur.deptName || "",
                    value: i,
                };
                if (cur.users && cur.users.length > 0) {
                    _cur.children = cur.users.map(subCur => {
                        const _subCur = {
                            label: subCur.name || "",
                            value: subCur.id || "",
                        };
                        if (this.data.actives.includes(_subCur.value)) {
                            _activeItems.push(_subCur);
                        }
                        return _subCur;
                    });
                }
                if (_cur.children && _cur.children.length > 0) {
                    _list.push(_cur);
                }
            }
            this.setData({
                items: _list,
                activeItems: _activeItems,
            });
        } catch (error) {
            console.log(error);
            // statements
            if (error.message) {
                wx.showToast({
                    mask: true,
                    title: error.message,
                    icon: "none",
                    duration: 2000,
                });
            } else {
                throw error;
            }
        }
    },
    buildTableData(list) {
        let res = [];
        if (list.length === 0) {
            return res;
        }
        res = list.map((cur, index) => {
            return {
                taskId: this.data.taskId,
                id: cur.id || "",
                cover: "",
                // 任务名称
                // name: cur.name || "",
                name: "数据项" + (index + 1),
                index: index,
                // 截至时间
                endTime: cur.finishTime || "",
                createTime: cur.createTime || "",
                source: cur,
            };
        });
        return res;
    },
    sure() {
        if (this.data.actives.length === 0) {
            wx.showToast({
                mask: true,
                title: "请选择转发人!",
                icon: "none",
                duration: 2000,
            });
            return;
        }
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.emit("sure", {
            actives: this.data.actives,
            items: this.data.activeItems,
        });
        this.cancel();
    },
    cancel() {
        wx.navigateBack();
    },
    onPullDownRefresh() {
        this.initData().finally(() => {
            wx.stopPullDownRefresh();
        });
    },
});
